package op_database;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import config.Configuration;
import config.ConfigManager;

import data_structure.Node;
import data_structure.NodeList;

 /**
 * @author HaoChen
 * DB_Operator 定义了相关数据库的读写I/O操作
 */
public class DB_Operator {
	private Connection con=null;
	private Statement st=null;
	private String server_ip=null;
	private int server_port=3306;
	private String dbname=null;
	private String table_name=null;
	private String username=null;
	private String psw=null;
	
	private Configuration config;
	/**
	 * 
	 */
	public DB_Operator(){
		init();
		connectToDB();
	}
	
	/**
	 * init();
	 * 初始化,获取初始配置信息
	 */
	public void init(){
		
		config=ConfigManager.Instance().getConfig();
		server_ip=config.getServer_ip();
		server_port=config.getServer_port();
		dbname=config.getDbname();
		table_name=config.getTable_name();
		username=config.getUsername();
		psw=config.getPsw();
		
	
	};
	/**
	 * 连接数据库
	 */
	public void connectToDB(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			con=DriverManager.getConnection("jdbc:mysql://"+server_ip+":"+server_port+"/"+dbname, username, psw);
			//System.out.println("Successful connect to db server!");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			st=con.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 读取数据库信息,返回Cell_list实例
	 * @return Cell_list;
	 */
	public  NodeList readData(){
		NodeList list=new NodeList();
		try {
			ResultSet rs=st.executeQuery("Select * from "+table_name+";");
		    while (rs.next()) {
		    		int id=rs.getInt("id");
		    		String symbol=rs.getString("symbol");
		    		String date=rs.getString("date");
		    		String time=rs.getString("time");
		    		double price=rs.getDouble("price");
		    		int size=rs.getInt("size");
		    		Node c=new Node();
		    		
		    		c.setName(symbol);
		    		c.setDate(date);
		    		c.setTime(time);
		    		c.setPrice(price);
		    		c.setSize(size);
		    		list.add_cell(c);
		    		System.out.println(id+" "+symbol+"　"+date+" "+time+" "+price+" "+size);
		    	  
		    		
		      }
		    return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * @param cell
	 * @return 
	 */
	public boolean insertData(Node cell){

		try {
			st.execute("Insert into "+dbname+"."+table_name+"(symbol,date,time,price,size) values ('"
					+cell.getName()+"','"+cell.getDate()+"','"+cell.getTime()+"','"+cell.getPrice()+"','"+cell.getSize()+
					"')");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}
	public void insertData(NodeList list){
		int i=0;
		while(i<list.getSize()){
			insertData(list.get(i));
		}
		
	}
	/**
	 * @param symbol
	 * @param date
	 * @param time
	 * @param price
	 * @param size
	 * @return
	 */
	public boolean insertData(String symbol,String date,String time,double price,int size){
		
		Node c=new Node();
		c.setName(symbol);
		c.setDate(date);
		c.setTime(time);
		c.setPrice(price);
		c.setSize(size);
		return insertData(c);
	};
	

	
}
